package com.qdu.study_app.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.qdu.study_app.Activity.Activity_clock;
import com.qdu.study_app.Activity.Activity_cpwd;
import com.qdu.study_app.Activity.Activity_index;
import com.qdu.study_app.Activity.Login_first;
import com.qdu.study_app.R;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cn.bmob.v3.BmobUser;

public class StudySetting extends Fragment {

    private Activity_index activity_index = new Activity_index();
    private Button logout;
    private Button cPwd;
    private TextView cur_user;
    private static Map<String, Activity> destroyMap = new HashMap<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.study_setting_fragment, container, false);
        logout = view.findViewById(R.id.logout_button);
        cPwd = view.findViewById(R.id.cpwdbutton);
        cur_user = view.findViewById(R.id.cur_user_id);
        cur_user.setText(BmobUser.getCurrentUser().getObjectId());

        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BmobUser.logOut();
                Intent intent = new Intent(getContext(), Login_first.class);
                getActivity().onBackPressed();
                startActivity(intent);
                getActivity().onBackPressed();
            }
        });
        cPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), Activity_cpwd.class);
                getActivity().onBackPressed();
                startActivity(intent);
                getActivity().onBackPressed();

            }
        });
        return view;
    }
    /**
     * 销毁指定Activity
     */
    public static void destroyActivity(String activityName) {
        Set<String> keySet = destroyMap.keySet();
        if (keySet.size() > 0) {
            for (String key : keySet) {
                if (activityName.equals(key)) {
                    destroyMap.get(key).finish();
                }
            }
        }
    }
}
